import fs from 'file-saver'
import XLSX from 'xlsx'

export function xlsx(json, fields, filename = '.xlsx',comment = null){
  json.forEach(item => {
    for (let i in item) {
      if(fields.hasOwnProperty(i)) {
        item[fields[i]] = item[i]
      }
      delete item[i]
    }
  })
  let sheetName = filename
  let wb = XLSX.utils.book_new()
  let ws = XLSX.utils.json_to_sheet(json,{header: Object.values(fields)})
  if(comment == 1){ //接入设置备注
    console.log(ws);
    var cell = [ws['A1'],ws['B1'],ws['C1'],ws['G1'],ws['I1'],ws['J1'],ws['K1'],ws['L1'],ws['M1'],ws['N1']]
    var commentPart = [
      {
        t: '监控点序号不可修改'
      },
      {
        t: '仅支持输入24位以内中英文、空格、数字、下划线、短横杠、点、小括号'
      },
      {
        t: '1: RTSP类型；0: ONVIF类型'
      },
      {
        t: '仅支持输入4~18位字母、数字、下划线、横杠!'
      },
      {
        t: '1: 启用；0: 关闭'
      },
      {
        t: '0: UDP协议；1: TCP协议'
      },
      {
        t: '分辨率可选范围：480P、720P、1080P、2K、4K'
      },
      {
        t: '1: H264；2: H265'
      },
      {
        t: '0: 固定码率模式；1: 动态码率模式；2: 平均动态码率模式；3: 固定量化系数模式'
      },
      {
        // t: '0: 若编码类型为H264，则编码档次为Base；若编码类型为H265,编码档次为MP；1: 若编码类型为H264,编码档次为MP；2: 若编码类型为H264,编码档次为HP'
        t: '当编码类型为1时，0：基本画质，1：主流画质,2: 高级画质；当编码类型为2时，1：主流画质'
        // t: '0：基本画质，1：主流画质,2: 高级画质'
      }
    ]
    cell.forEach((item,index)=>{
      if(!item || !item.c) item.c = []
      item.c.hidden = true
      item.c.push(commentPart[index])
    })
  }else if(comment == 2){//上级域注释
    console.log(ws);
    var cell = [ws['A1'],ws['B1'],ws['C1']]
    var commentPart = [
      {
        t: '仅支持输入24位以内中英文、空格、数字、下划线、短横杠、点、小括号'
      },
      {
        t: 'SIP服务器ID需为16到20位数字'
      },
      {
        t: 'SIP服务器域需为10位数字'
      }
    ]
    cell.forEach((item,index)=>{
      if(!item || !item.c) item.c = []
      item.c.hidden = true
      item.c.push(commentPart[index])
    })
  }else if(comment == 3){//本域注释
    console.log(ws);
    var cell = [ws['A1'],ws['B1']]
    var commentPart = [
      {
        t: '用户名需为16到20位数字'
      },
      {
        t: '仅支持输入4~18位字母、数字、下划线、横杠!'
      },
    ]
    cell.forEach((item,index)=>{
      if(!item || !item.c) item.c = []
      item.c.hidden = true
      item.c.push(commentPart[index])
    })
  }else if(comment == 4){//本域通道注释
    console.log(ws);
    var cell = [ws['A1'],ws['B1'],ws['C1']]
    var commentPart = [
      {
        t: '序号不可删除和修改'
      },
      {
        t: '仅支持24位以内中英文空格、数字、下划线、短横杠、点、小括号'
      },
      {
        t: '16到20位数字'
      },
    ]
    cell.forEach((item,index)=>{
      if(!item || !item.c) item.c = []
      item.c.hidden = true
      item.c.push(commentPart[index])
    })
  }


  wb.SheetNames.push(sheetName)
  wb.Sheets[sheetName] = ws
  const defaultCellStyle = {
    font: {
      name: 'Verdana',
      sz: 13,
      color: 'FF00FF88'
    },
    width: 200
  }
  let wopts = {
    bookType: 'xlsx',
    bookSST: false,
    type: 'binary',
    cellStyles: true,
    defaultCellStyle: defaultCellStyle,
    showGridLines: false
  }
  let wabout = XLSX.write(wb,wopts)
  let blob = new Blob([s2ab(wabout)],{type: 'application/octet-stream'})
  fs.saveAs(blob, filename + '.xlsx')
}

const s2ab = s => {
  var buf;
  if(typeof ArrayBuffer !== 'undefined'){
    buf = new ArrayBuffer(s.length)
    var view = new Uint8Array(buf)
    for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xff
    return buf
  }else{
    buf = new Array(s.length)
    for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
    return buf;
  }
}
